package com.octnote.controller;

import com.octnote.domain.note.NoteCreateForm;
import com.octnote.domain.note.UserNote;
import com.octnote.message.Message;
import com.octnote.service.NoteService;
import com.octnote.service.SendSMSService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 处理记事类接口
 * Created by leohehe on 2017/4/11.
 */
@RestController
@PropertySource("classpath:message.properties")
@RequestMapping(value = "/api/v1/", name = "记事API")
public class NoteController {
    private static final Logger LOGGER = LoggerFactory.getLogger(NoteController.class);
    private SendSMSService sendSMSService;
    private Message message = new Message();
    private NoteService noteService;

    @Autowired
    public NoteController(SendSMSService sendSMSService, NoteService noteService){
        this.sendSMSService = sendSMSService;
        this.noteService = noteService;
    }

    @ResponseBody
    @RequestMapping(value = "i/note/creatNote", method = RequestMethod.POST)
    public ResponseEntity<Message> createNote(@Valid @RequestBody NoteCreateForm form,
                                              BindingResult bindingResult, HttpServletRequest request){
        try {
            UserNote userNote = noteService.create(form,request);
            message.setMsg(1, "记事创建成功", userNote);
            return new ResponseEntity<Message>(message, HttpStatus.OK);
        } catch (DataIntegrityViolationException e) {
            LOGGER.warn("create news error", e);
            message.setMsg(0, "创建记事失败");
            return new ResponseEntity<Message>(message, HttpStatus.OK);
        }
    }

    @ResponseBody
    @RequestMapping(value = "i/note/modifyNote", method = RequestMethod.POST)
    public ResponseEntity<Message> modifyNote(@Valid @RequestBody NoteCreateForm form,
                                              BindingResult bindingResult, HttpServletRequest request){
        try {
            UserNote userNote = noteService.create(form,request);
            message.setMsg(1, "记事修改成功",userNote);
            return new ResponseEntity<Message>(message, HttpStatus.OK);
        } catch (DataIntegrityViolationException e) {
            LOGGER.warn("create news error", e);
            message.setMsg(0, "修改记事失败");
            return new ResponseEntity<Message>(message, HttpStatus.OK);
        }
    }

    @ResponseBody
    @RequestMapping(value = "i/note/delteNote/{id}", method = RequestMethod.POST)
    public ResponseEntity<Message> modifyNote(@PathVariable long id){
        try {
            noteService.delete(id);
            message.setMsg(1, "记事删除成功");
            return new ResponseEntity<Message>(message, HttpStatus.OK);
        } catch (DataIntegrityViolationException e) {
            LOGGER.warn("create news error", e);
            message.setMsg(0, "删除记事失败");
            return new ResponseEntity<Message>(message, HttpStatus.OK);
        }
    }

    /**
     * 获取记事列表
     * @param searchPhrase
     * @param p page=1&size=11&sort=title,desc
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "i/note/getNoteList", method = RequestMethod.GET)
    public ResponseEntity<Message> getNoteList(@RequestParam(required = false) String searchPhrase,Pageable p){
        Page<UserNote> userNotes = noteService.getNoteList(searchPhrase, p);
        message.setMsg(1, "获取记事列表成功", userNotes);
        return new ResponseEntity<Message>(message, HttpStatus.OK);
    }

    /**
     * 记录一个消息
     */
    private void setNoteMessage(){

    }
}
